//
//  ProductDetailViewController.m
//  NOAHWM
//
//  Created by Ryan on 13-6-4.
//  Copyright (c) 2013年 Ryan. All rights reserved.
//

#import "ProductDetailViewController.h"
#import "NYMemberCenterDataManager.h"
#import "JSON.h"
#import "AuthViewController.h"
#import "YuYueViewController.h"
#import "YuYueListViewController.h"
#import "CancelYuYueViewController.h"
#import "AccountProductDetailCell.h"
#import "ProductDetailTableCell.h"
#import "MenuDataManager.h"
#import "NYProductAd.h"

#define kProductTypeFieldMapFileName @"ProductTypeMap"

#define kGapBetweenView 10.f
#define kButtonHeight 40.f
#define kButtonWidth 149.f

#define kYuyueButtonTitle     @"预约"
#define kMyYuyueButtonTitle    @"我的预约"
#define kTouristRight                @"TouristAuthority"
#define kRegMemberRight              @"RegMemberAuthority"
#define kAuthMemberRight             @"AuthMemberAuthority"

#define kProDetailDownloaderKey     @"ProDetailDownloaderKey"
#define kProCollectDownloaderKey    @"ProCollectDownloaderKey"
#define kYuyueStatusFileName        @"YuYueStatus"

@interface ProductDetailViewController ()

@property (nonatomic, retain) NSDictionary *yuyueStatusDict;
@property (nonatomic, retain) NSArray *fieldArray;

@end

@implementation ProductDetailViewController

@synthesize moreButton,yuyueButton,contentTableView,editButton,cancelButton;
@synthesize product,productDetail;
@synthesize muArray;
@synthesize isForYuYue;
@synthesize yuyueStatusDict;
@synthesize fieldArray;
@synthesize imagesContainView;
@synthesize placeHolderImageView;
@synthesize isForCollect;

- (ImagesContainView *)imagesContainView
{
    if(nil == imagesContainView)
    {
        NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"ImagesContainView" owner:self options:nil];
        imagesContainView = [[nibs lastObject] retain];
    }
    return imagesContainView;
}

#pragma mark - IBAction methods
- (IBAction)moreButtonClicked:(id)sender
{
    //跳转申请认证页面
    AuthViewController *avc = [[AuthViewController alloc] init];
    avc.isMenu = NO;
    [self.navigationController pushViewController:avc animated:YES];
    [avc release];
}

- (IBAction)yuyueButtonClicked:(id)sender
{
    if([[yuyueButton titleForState:UIControlStateNormal] isEqualToString:kYuyueButtonTitle])
    {
        YuYueViewController *yyvc = [[YuYueViewController alloc] init];
        yyvc.isEditYuyue = NO;
        yyvc.productDetail = self.productDetail;
        [self.navigationController pushViewController:yyvc animated:YES];
        [yyvc release];
    }
    else if([[yuyueButton titleForState:UIControlStateNormal] isEqualToString:kMyYuyueButtonTitle])
    {
        YuYueListViewController *yylvc = [[YuYueListViewController alloc] init];
        yylvc.yuyueEnter = kDetailPageEnter;
        [self.navigationController pushViewController:yylvc animated:YES];
        [yylvc release];
    }
}

- (IBAction)editButtonClicked:(id)sender
{
    YuYueViewController *yyvc = [[YuYueViewController alloc] init];
    yyvc.isEditYuyue = YES;
    yyvc.productDetail = self.productDetail;
    [self.navigationController pushViewController:yyvc animated:YES];
    [yyvc release];
}

- (IBAction)cancelButtonClicked:(id)sender
{
    CancelYuYueViewController *cyyvc = [[CancelYuYueViewController alloc] init];
    cyyvc.productDetail = self.productDetail;
    [self.navigationController pushViewController:cyyvc animated:YES];
    [cyyvc release];
}

#pragma mark - BaseViewController methods
- (void)leftItemTapped
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)rightItemTapped
{
    NSArray *properties = nil;
    if([self.productDetail.iscancollection isEqualToString:@"1"])
    {
        if(!self.isForYuYue)
            properties = [NSArray arrayWithObjects:@"Call",@"Collect",nil];
        else
            properties = [NSArray arrayWithObject:@"Call"];
    }
    else
    {
        if(!self.isForYuYue)
            properties = [NSArray arrayWithObjects:@"Call",@"NoCollect",nil];
        else
            properties = [NSArray arrayWithObject:@"Call"];
    }
    NSMutableDictionary *filterInfo = [NSMutableDictionary dictionary];
    [filterInfo setObject:properties forKey:kFilterPropertiesKey];
    [filterInfo setObject:[NSNumber numberWithInt:kLocationRight] forKey:kFilterLocationType];
    [filterInfo setObject:[NSNumber numberWithInteger:kProDetailFilter] forKey:kFilterTypeKey];
    [[NSNotificationCenter defaultCenter] postNotificationName:kShowFilterViewNotification object:nil userInfo:filterInfo];
}

#pragma mark - Notification methods
- (void)showYuyueDetailViewWithNotification:(NSNotification *)notification
{
    if(nil != notification.object)
    {
        //从预约列表进入当前页
        self.product = notification.object;
        self.isForYuYue = YES;
    }
    //从认证页返回当前页
    [self reloadProductDetail];
    [self.navigationController popToViewController:self animated:YES];
}

- (void)collectProductWithNotification:(NSNotification *)notification
{
    NSString *suburl = nil;
    if([self.productDetail.iscancollection isEqualToString:@"1"])
    {
        suburl = [NSString stringWithFormat:kCollectProUrl,@"jsonp",[NYMemberCenterDataManager sharedManager].loginMember.accountId,self.productDetail.productbooking_id];
        suburl = [suburl stringByAppendingString:@"app000033"];
    }
    else
    {
        suburl = [NSString stringWithFormat:kCancelCollectProUrl,@"jsonp",[NYMemberCenterDataManager sharedManager].loginMember.accountId,self.productDetail.collection_id];
        suburl = [suburl stringByAppendingString:@"app000034"];
    }
    NSString *url = [NSString stringWithFormat:@"%@%@",kServerAddress,suburl];
    url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [[RYDownloaderManager sharedManager] requestDataByGetWithURLString:url delegate:self purpose:kProCollectDownloaderKey];
}

#pragma mark - UIViewController methods

- (void)reloadProductDetail
{
    [self setNaviTitle:self.product.public_product_name];
    NSString *accountId = nil;
    if(nil == [NYMemberCenterDataManager sharedManager].loginMember.accountId)
        accountId = @"";
    else
        accountId = [NYMemberCenterDataManager sharedManager].loginMember.accountId;
    NSString *suburl = nil;
    
    //如果productbooking_id=nil则是从预约列表进入该页面
    //否则是从产品列表进入该页面
    if(self.isForYuYue)
    {
        suburl = [NSString stringWithFormat:kYuyueDetailUrl,@"jsonp",self.product.sf_opportunity_id,@""];
        suburl = [suburl stringByAppendingString:@"app000041"];
    }
    else
    {
        suburl = [NSString stringWithFormat:kProductDetailUrl,@"jsonp",accountId,self.product.productbooking_id,@""];
        if(self.isForCollect)
            suburl = [suburl stringByAppendingString:@"app000025"];
        else
            suburl = [suburl stringByAppendingString:@"app000035"];
    }
    NSString *url = [NSString stringWithFormat:@"%@%@",kServerAddress,suburl];
    url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [[RYDownloaderManager sharedManager] requestDataByGetWithURLString:url delegate:self purpose:kProDetailDownloaderKey];
    
    //清空所有数据
    self.muArray = [NSMutableArray array];
    self.contentTableView.tableHeaderView = nil;
    [self.contentTableView reloadData];
    
    CGRect tableRect = self.contentTableView.frame;
    tableRect.size.height = self.view.frame.size.height-tableRect.origin.y;
    self.contentTableView.frame = tableRect;
    
    self.moreButton.hidden = YES;
    self.yuyueButton.hidden = YES;
    self.editButton.hidden = YES;
    self.cancelButton.hidden = YES;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setLeftNaviItemWithTitle:nil imageName:@"Icon_Back"];
    [self setRightNaviItemWithTitle:nil imageName:@"Icon_Dial"];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showYuyueDetailViewWithNotification:) name:kShowYuyueDetailViewNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(collectProductWithNotification:) name:kCollectProductNotification object:nil];
    
    NSString *path = [[NSBundle mainBundle] pathForResource:kYuyueStatusFileName ofType:@"plist"];
    self.yuyueStatusDict = [NSDictionary dictionaryWithContentsOfFile:path];
    self.placeHolderImageView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_rightMenu@2x.png"]];
    [self reloadProductDetail];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    if(self.isViewLoaded && !self.view.window)
    {
        [[RYDownloaderManager sharedManager] cancelDownloaderWithDelegate:self purpose:nil];
        [[NSNotificationCenter defaultCenter] removeObserver:self];
        self.view = nil;
        self.moreButton = nil;
        self.yuyueButton = nil;
        self.contentTableView = nil;
        self.muArray = nil;
        self.editButton = nil;
        self.cancelButton = nil;
        self.imagesContainView = nil;
        self.placeHolderImageView = nil;
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    [[RYDownloaderManager sharedManager] cancelDownloaderWithDelegate:self purpose:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    self.moreButton = nil;
    self.yuyueButton = nil;
    self.contentTableView = nil;
    self.muArray = nil;
    self.editButton = nil;
    self.cancelButton = nil;
    self.imagesContainView = nil;
    self.placeHolderImageView = nil;
}

- (void)dealloc
{
    [[RYDownloaderManager sharedManager] cancelDownloaderWithDelegate:self purpose:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [placeHolderImageView release];
    [imagesContainView release];
    [fieldArray release];
    [yuyueStatusDict release];
    [moreButton release];
    [yuyueButton release];
    [product release];
    [productDetail release];
    [contentTableView release];
    [muArray release];
    [editButton release];
    [cancelButton release];
    [super dealloc];
}
#pragma mark - RYDownloaderDelegate methods
- (void)downloader:(RYDownloader*)downloader completeWithNSData:(NSData*)data
{
    NSString *str = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
    str = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    //去掉被包的jsonp字串
    if(str.length > 7)
        str = [str substringWithRange:NSMakeRange(6, str.length-7)];
    if([downloader.purpose isEqualToString:kProDetailDownloaderKey])
    {
        NSDictionary *dict = [str JSONValue];
        if([[dict objectForKey:@"result"] isEqualToString:@"1"])
        {
            self.placeHolderImageView.hidden = YES;
            NSString *mapPath = [[NSBundle mainBundle] pathForResource:kProductTypeFieldMapFileName ofType:@"plist"];
            NSDictionary *mapDict = [NSDictionary dictionaryWithContentsOfFile:mapPath];
            NSString *typeFileName = [mapDict objectForKey:[dict objectForKey:@"type_customer"]];
            NSString *productFeildPath = [[NSBundle mainBundle] pathForResource:typeFileName ofType:@"plist"];
            self.fieldArray = [NSArray arrayWithContentsOfFile:productFeildPath];
            
            NSString * logStatus = nil;
            MemberType memberType = [NYMemberCenterDataManager sharedManager].loginMember.memberType;
            if (memberType == kMemberRegister)
                logStatus = kRegMemberRight;
            else if (memberType == kMemberAuthentication)
                logStatus = kAuthMemberRight;
            else
                logStatus = kTouristRight;
            
            //保存所有值到NYProductDetail实例中
            NYProductDetail *pd = [[NYProductDetail alloc] init];
            for(NSString *key in [dict allKeys])
            {
                id value = [dict objectForKey:key];
                @try {
                    [pd setValue:value forKey:key];
                }
                @catch (NSException *exception) {
                    NSLog(@"试图添加不存在的key: %@到NYProductDetail实例中.",key);
                }
            }
            self.productDetail = pd;
            [pd release];
            //保存当前权限字段
            NSMutableArray *tempArray = [NSMutableArray array];
            for(NSDictionary *dictField in self.fieldArray)
            {
                if([[dictField objectForKey:logStatus] boolValue])
                {
                    [tempArray addObject:dictField];
                }
            }
            //重新整理自定义字段
            NSInteger index = 0;
            for(NSDictionary *fieldDict in tempArray)
            {
                if([[fieldDict objectForKey:@"filedName"] isEqualToString:@"otherlist"])
                {
                    break;
                }
                index++;
            }
            if(index != tempArray.count)
            {
                //存在otherlist
                [tempArray removeObjectAtIndex:index];
                for(NSDictionary *dict in self.productDetail.otherlist)
                {
                    NSMutableDictionary *localFiledDict = [NSMutableDictionary dictionary];
                    [localFiledDict setValue:[dict objectForKey:@"fremark"] forKey:@"filedName"];
                    [localFiledDict setValue:[dict objectForKey:@"fapp_fldname"] forKey:@"CNName"];
                    [localFiledDict setValue:[NSNumber numberWithBool:YES] forKey:@"TouristAuthority"];
                    [localFiledDict setValue:[NSNumber numberWithBool:YES] forKey:@"RegMemberAuthority"];
                    [localFiledDict setValue:[NSNumber numberWithBool:YES] forKey:@"AuthMemberAuthority"];
                    [localFiledDict setValue:[NSNumber numberWithBool:NO] forKey:@"MutipleLines"];
                    [localFiledDict setValue:[NSNumber numberWithBool:YES] forKey:@"IsCustomField"];
                    [tempArray insertObject:localFiledDict atIndex:index];
                    index++;
                }
            }
            self.muArray = tempArray;
            //判断当前产品是否可以被预约，如果可以则enable预约按钮，否则disable预约按钮
            NSInteger buttonRow = 0;
            
            //设置更多按钮元素状态
            NYElement *nye = nil;
            for(NYElement *element in [MenuDataManager sharedManager].elements)
            {
                if([element.elementId isEqualToString:kOnlineProductDetailMoreButtonId])
                {
                    nye = element;
                    break;
                }
            }
            if(nye.elementAccess == kElementVisiableEnable && ![[NYMemberCenterDataManager sharedManager].loginMember.is_auth isEqualToString:@"1"])
            {
                //more按钮可见可点击
                self.moreButton.hidden = NO;
                buttonRow++;
            }
            else if(nye.elementAccess == kElementVisiableDisable && ![[NYMemberCenterDataManager sharedManager].loginMember.is_auth isEqualToString:@"1"])
            {
                //more按钮可见不可点
                self.moreButton.hidden = NO;
                buttonRow++;
            }
            else
            {
                //more按钮不可见
                self.moreButton.hidden = YES;
            }
            
//            if([NYMemberCenterDataManager sharedManager].loginMember.memberType == kMemberRegister &&
//               ![[NYMemberCenterDataManager sharedManager].loginMember.is_auth isEqualToString:@"1"])
//            {
//                //如果是注册会员同时未申请过认证
//                self.moreButton.hidden = NO;
//                buttonRow++;
//            }
//            else
//            {
//                //认证会员或者已申请过认证
//                self.moreButton.hidden = YES;
//            }
            
            if(self.isForYuYue)
            {
                //从预约列表进入
                self.yuyueButton.hidden = YES;
                //从本地的预约状态对应表来判断是否能够修改/取消
                NSString *statusValue = [self.yuyueStatusDict objectForKey:self.productDetail.stagename];
                if(statusValue)
                {
                    NSArray *array = [statusValue componentsSeparatedByString:@"/"];
                    BOOL canEdit = (BOOL)[[array objectAtIndex:0] integerValue];
                    BOOL canCancel = (BOOL)[[array objectAtIndex:1]integerValue];
                    self.editButton.hidden = !canEdit;
                    self.cancelButton.hidden = !canCancel;
                    if(canEdit || canCancel)
                    {
                        buttonRow++;
                        if(canEdit && canCancel)
                        {
                            CGRect editRect = self.editButton.frame;
                            editRect.size.width = kButtonWidth;
                            self.editButton.frame = editRect;
                            [self.editButton setBackgroundImage:[UIImage imageNamed:@"bg_half_button_normal@2x.png"] forState:UIControlStateNormal];
                            [self.editButton setBackgroundImage:[UIImage imageNamed:@"bg_half_button_highlighted@2x.png"] forState:UIControlStateHighlighted];
                            
                            CGRect cancelRect = self.cancelButton.frame;
                            cancelRect.origin.x = self.view.frame.size.width - kGapBetweenView-kButtonWidth;
                            cancelRect.size.width = kButtonWidth;
                            self.cancelButton.frame = cancelRect;
                            [self.cancelButton setBackgroundImage:[UIImage imageNamed:@"bg_half_button_normal@2x.png"] forState:UIControlStateNormal];
                            [self.cancelButton setBackgroundImage:[UIImage imageNamed:@"bg_half_button_highlighted@2x.png"] forState:UIControlStateHighlighted];
                        }
                        else if(canEdit && !canCancel)
                        {
                            self.editButton.frame = self.yuyueButton.frame;
                            [self.editButton setBackgroundImage:[UIImage imageNamed:@"bg_whole_button_normal@2x.png"] forState:UIControlStateNormal];
                            [self.editButton setBackgroundImage:[UIImage imageNamed:@"bg_whole_button_highlighted@2x.png"] forState:UIControlStateHighlighted];
                        }
                        else
                        {
                            self.cancelButton.frame = self.yuyueButton.frame;
                            [self.cancelButton setBackgroundImage:[UIImage imageNamed:@"bg_whole_button_normal@2x.png"] forState:UIControlStateNormal];
                            [self.cancelButton setBackgroundImage:[UIImage imageNamed:@"bg_whole_button_highlighted@2x.png"] forState:UIControlStateHighlighted];
                        }
                    }
                }
                else
                {
                    self.editButton.hidden = YES;
                    self.cancelButton.hidden = YES;
                }
            }
            else
            {
                //从在线产品列表进入
                if(![self.productDetail.iscanbooking isEqualToString:@"1"] &&
                   ![self.productDetail.isbooking isEqualToString:@"1"])
                {
                    self.yuyueButton.hidden = YES;
                }
                else
                {
                    self.yuyueButton.hidden = NO;
                    buttonRow++;
                    if([self.productDetail.iscanbooking isEqualToString:@"1"])
                    {
                        [self.yuyueButton setTitle:kYuyueButtonTitle forState:UIControlStateNormal];
                    }
                    else if([self.productDetail.isbooking isEqualToString:@"1"])
                    {
                        [self.yuyueButton setTitle:kMyYuyueButtonTitle forState:UIControlStateNormal];
                    }
                }
                self.editButton.hidden = YES;
                self.cancelButton.hidden = YES;
            }
            
            CGRect tableRect = self.contentTableView.frame;
            if(buttonRow == 0)
            {
                tableRect.size.height = self.view.frame.size.height - tableRect.origin.y;
            }
            else
            {
                tableRect.size.height = self.view.frame.size.height - tableRect.origin.y - buttonRow*kButtonHeight-(buttonRow+1)*kGapBetweenView;
            }

            self.contentTableView.frame = tableRect;
            
//            if([NYMemberCenterDataManager sharedManager].loginMember.memberType == kMemberAuthentication)
//            {
                //只有认证会员才能看到顶部的图片列表
                NSArray *images = self.productDetail.img_url_list;
                if(images.count > 0)
                {
                    NSMutableArray *productAds = [NSMutableArray array];
                    for(NSString *imageUrl in images)
                    {
                        NYProductAd *productAd = [[NYProductAd alloc] init];
                        productAd.image_url = imageUrl;
                        productAd.remark = @"";
                        [productAds addObject:productAd];
                        [productAd release];
                    }
                    [self.imagesContainView reloadWithProductAds:productAds];
                    self.contentTableView.tableHeaderView = self.imagesContainView;
                }
                else
                    self.contentTableView.tableHeaderView = nil;
//            }
            
            [self.contentTableView reloadData];
        }
        else
        {
            self.placeHolderImageView.hidden = NO;
            self.placeHolderImageView.image = [UIImage imageNamed:@"nodata.png"];
            [[RYHUDManager sharedManager] showWithMessage:[dict objectForKey:@"message"] customView:nil hideDelay:2.f];
        }
    }
    else
    {
        NSDictionary *dict = [str JSONValue];
        if([[dict objectForKey:@"result"] isEqualToString:@"1"])
        {
            NSString *collectID = [NSString stringWithFormat:@"%@",self.productDetail.collection_id];
            [[NSNotificationCenter defaultCenter] postNotificationName:kmyKeepFreshNotification object:collectID];
            if([self.productDetail.iscancollection isEqualToString:@"1"])
            {
                self.productDetail.collection_id = [dict objectForKey:@"collection_pkid"];
                self.productDetail.iscancollection = @"0";
            }
            else
            {
                self.productDetail.iscancollection = @"1";
                self.productDetail.collection_id = @"";
            }
        }
        [[RYHUDManager sharedManager] showWithMessage:[dict objectForKey:@"message"] customView:nil hideDelay:2.f];
    }
}
- (void)downloader:(RYDownloader*)downloader didFinishWithError:(NSString*)message
{
    if([downloader.purpose isEqualToString:kProDetailDownloaderKey])
    {
        self.placeHolderImageView.image = [UIImage imageNamed:@"nodata.png"];
        self.placeHolderImageView.hidden = NO;
    }
    [[RYHUDManager sharedManager] showWithMessage:kNetWorkErrorString customView:nil hideDelay:2.f];
}

#pragma mark - UITableViewDataSource methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.muArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dict = [self.muArray objectAtIndex:indexPath.row];
    if([[dict objectForKey:@"MutipleLines"] boolValue])
    {
        static NSString *mutipleCellIdentify = @"mutipleCellIdentify";
        ProductDetailTableCell *mutipleCell = [tableView dequeueReusableCellWithIdentifier:mutipleCellIdentify];
        if(nil == mutipleCell)
        {
            NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"ProductDetailTableCell" owner:self options:nil];
            mutipleCell = [nibs lastObject];
            mutipleCell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        mutipleCell.keyLabel.text = [dict objectForKey:@"CNName"];
        NSString *field = [dict objectForKey:@"filedName"];
        
        NSString *filedValue = @"";
        if([self.productDetail respondsToSelector:NSSelectorFromString(field)])
            filedValue = [NSString stringWithFormat:@"%@",[self.productDetail valueForKey:field]];
        mutipleCell.valueLabel.text = filedValue;
        
        if(indexPath.row %2 == 0)
            mutipleCell.bgImageView.hidden = NO;
        else
            mutipleCell.bgImageView.hidden = YES;
        
        return mutipleCell;
    }
    else
    {
        static NSString *cellIdentify = @"cellIdentify";
        AccountProductDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
        if(nil == cell)
        {
            NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"AccountProductDetailCell" owner:self options:nil];
            cell = [nibs lastObject];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.keyLabel.text = [dict objectForKey:@"CNName"];
        NSString *filedValue = @"";
        if([[dict objectForKey:@"IsCustomField"] boolValue])
        {
            filedValue = [dict objectForKey:@"filedName"];
        }
        else
        {
            NSString *field = [dict objectForKey:@"filedName"];
            if([self.productDetail respondsToSelector:NSSelectorFromString(field)])
                filedValue = [NSString stringWithFormat:@"%@",[self.productDetail valueForKey:field]];
        }
        cell.valueLabel.text = filedValue;
        if(indexPath.row %2 == 0)
            cell.bgImageView.hidden = NO;
        else
            cell.bgImageView.hidden = YES;
        
        return cell;
    }
}

#pragma mark - UITableViewDelegate methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dict = [self.muArray objectAtIndex:indexPath.row];
    if([[dict objectForKey:@"MutipleLines"] boolValue])
    {
        NSString *filedKey = [dict objectForKey:@"filedName"];
        if([self.productDetail respondsToSelector:NSSelectorFromString(filedKey)])
        {
            NSString *filedValue = [NSString stringWithFormat:@"%@",[self.productDetail valueForKey:filedKey]];
//            filedValue = [filedValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
            CGSize tempSize = CGSizeMake(kProductDetailCellValueTopWidth, 2000.f);
            CGSize actualSize = [filedValue sizeWithFont:kProductDetailCellValueFont constrainedToSize:tempSize lineBreakMode:NSLineBreakByWordWrapping];
            return kProductDetailCellValueTopMargin+actualSize.height;
        }
        else
        {
            return kProductDetailCellValueTopMargin;
        }
    }
    else
    {
        return 40.f;
    }
}
@end
